function a(ele){return document.querySelector(ele)}
function c(ele){return document.createElement(ele)}

//ajax_get
function ajax_get(url){
    return new Promise((resolve,reject)=>{
        let xhr=new XMLHttpRequest()
        xhr.open("get",url,true)
        xhr.send()
        xhr.onreadystatechange=()=>{
            if(xhr.readyState === 4 && xhr.status === 200){
                resolve(xhr.responseText)
            }
        }
    })
}
//ajax_post
function ajax_post(url,str){
    return new Promise((resolve,reject)=>{
        let xhr=new XMLHttpRequest()
        xhr.open("post",url,true)
        xhr.setRequestHeader('content-type','application/x-www-form-urlencoded')
        xhr.send(str)
        xhr.onreadystatechange=()=>{
            if(xhr.readyState === 4 && xhr.status === 200){
                resolve(xhr.responseText)
            }
        }
    })
}

window.onload=function(){
    let url=decodeURI(decodeURI(window.location.href))
    if(url.includes("?")){
        let url_str=url.split("?")[1]
        let type=url_str.split("=")[1]
        create_type(type)
    }else{
        let all=JSON.parse(localStorage.getItem("local_res"))
        create_all(all)
    }
}

function create_type(type){
    a("section").innerHTML=""
    let arr=JSON.parse(localStorage.getItem("local_res"))
    let newArr=[]
    newArr=arr.filter((item)=>{
        return item.type == type
    })
    for(let i of newArr){
        let content=$("<div></div>")
        content.addClass("content")
        let content_a1=$("<a></a>")
        let img=$("<img></img>")
        img.attr("src","./img/"+i.id+".jpg").addClass("content-img1")
        content_a1.addClass("content-a1").append($(img))
        let product_name=$("<div></div>")
        product_name.addClass("product_name").text(i.product_name)
        let product_price=$("<div></div>")
        product_price.addClass("product_price").text("￥"+i.product_price)
        let form=$("<form></form>")
        let star=$("<input>")
        star.attr("id","star").val("收藏").attr("tag",i.id).attr("type","button")
        star.on("click",function(){
            let a=JSON.parse(localStorage.getItem("local_star"))
            let content_star={
                collect:$(this).attr("tag")
            }
            if(a.length <= 0){
                a.push(content_star)
            }else{
               if(!a.find((item)=>{return item.collect == content_star.collect})){
                   a.push(content_star)
               }
            }
            window.location.href="http://localhost/mogujie/star.html"
            localStorage.setItem("local_star",JSON.stringify(a))
        })
        let buy=$("<input>")
        buy.attr("id","buy").val("立即购买").attr("tag",i.id).attr("type","button")
        buy.on("click",function(){
            let a=JSON.parse(localStorage.getItem("local_collect"))
            let content_collect={
                shop_cart:$(this).attr("tag")
            }
            if(a.length <= 0){
                a.push(content_collect)
            }else{
                if(!a.find((item)=>{return item.shop_cart == content_collect.shop_cart})){
                    a.push(content_collect)
                }
            }
            localStorage.setItem("local_collect",JSON.stringify(a))
            window.location.href="http://localhost/mogujie/shop-cart.html?reload"
        })
        form.append($(star)).append($(buy)).attr("id","form")
        content.append($(content_a1)).append($(product_name)).append($(product_price)).append($(form))
        $("section").append($(content))   
    }
}

function create_all(all){
    a("section").innerHTML=""
    for(let i of all){
        let content=$("<div></div>")
        content.addClass("content")
        let content_a1=$("<a></a>")
        let img=$("<img></img>")
        img.attr("src","./img/"+i.id+".jpg").addClass("content-img1")
        content_a1.addClass("content-a1").append($(img))
        let product_name=$("<div></div>")
        product_name.addClass("product_name").text(i.product_name)
        let product_price=$("<div></div>")
        product_price.addClass("product_price").text("￥"+i.product_price)
        let form=$("<form></form>")
        let star=$("<input>")
        star.attr("id","star").val("收藏").attr("tag",i.id).attr("type","button")
        star.on("click",function(){
            let a=JSON.parse(localStorage.getItem("local_star"))
            let content_star={
                collect:$(this).attr("tag")
            }
            a.push(content_star)
            localStorage.setItem("local_star",JSON.stringify(a))
            window.location.href="http://localhost/mogujie/star.html"
        })
        let buy=$("<input>")
        buy.attr("id","buy").val("立即购买").attr("tag",i.id).attr("type","button")
        buy.on("click",function(){
            let a=JSON.parse(localStorage.getItem("local_collect"))
            let content_collect={
                shop_cart:$(this).attr("tag")
            }
            a.push(content_collect)
            localStorage.setItem("local_collect",JSON.stringify(a))
            window.location.href="http://localhost/mogujie/shop-cart.html?reload"
        })
        form.append($(star)).append($(buy)).attr("id","form")
        content.append($(content_a1)).append($(product_name)).append($(product_price)).append($(form))
        $("section").append($(content))   
    }
}